using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[System.Serializable]
public struct PosColor
{
    public Vector2Int pos;
    public Color color;
    public PosColor(Vector2Int pos,Color color)
    {
        this.pos = pos;
        this.color = color;
    }
    public PosColor(int x, int y,Color color)
    {
        this.pos = new Vector2Int(x, y);
        this.color = color;
    }
    public PosColor(int x, int y, float r,float g,float b)
    {
        this.pos = new Vector2Int(x, y);
        this.color = new Color(r, g, b);
    }
    public PosColor(int x, int y, float r, float g, float b,float a)
    {
        this.pos = new Vector2Int(x, y);
        this.color = new Color(r, g, b,a);
    }
    public PosColor(Vector2Int pos, float r, float g, float b, float a)
    {
        this.pos = pos;
        this.color = new Color(r, g, b, a);
    }
    public PosColor(Vector2Int pos, float r, float g, float b)
    {
        this.pos = pos;
        this.color = new Color(r, g, b);
    }

    public override bool Equals(object obj)
    {
        PosColor o = (PosColor)obj;
        return o.pos == this.pos;
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
}
public class Texture2D_工具
{
    public static Texture2D 初始化图片(Color 背景色,int 宽,int 高)
    {
        Texture2D 返回 = new Texture2D(宽, 高);
        for (int i = 0; i < 宽; i++)
        {
            for (int j = 0; j < 高; j++)
            {
                返回.SetPixel(i, j, 背景色);
            }
        }
        return 返回;
    }

    public static Texture2D 绘制点(Texture2D 图片, PosColor[] 坐标颜色,int 笔尖像素)
    {
        foreach (var 坐标原点 in 坐标颜色)
        {
            PosColor[] 笔尖 = 笔尖坐标(笔尖像素, 坐标原点);
            foreach (var item in 笔尖)
            {
                Color bgColor = 图片.GetPixel((int)item.pos.x, (int)item.pos.y);
                Color final_color = Color.Lerp(bgColor, item.color, item.color.a / 1.0f);
                图片.SetPixel((int)item.pos.x, (int)item.pos.y, final_color);
            }
        }
        图片.Apply();
        return 图片;
    }

    private static PosColor[] 笔尖坐标(int 像素,PosColor 坐标颜色)
    {
        List<PosColor> 返回 = new List<PosColor>();

        if (像素 == 1) return new PosColor[1] { 坐标颜色 };

        int 小数 = 像素 % 2 == 0 ? 像素 / 2 -1 : 像素 / 2;
        if (像素 == 3) 小数 = 1;
        if (像素 == 2) 小数 = 0;
        小数 = -小数;

        for (int x = 小数; x <= 像素/2; x++)
        {
            for (int y = 小数; y <= 像素/2; y++)
            {
                返回.Add(new PosColor(坐标颜色.pos.x + x, 坐标颜色.pos.y + y, 坐标颜色.color));
            }
        }
        

        return 返回.ToArray();
    }

    public static IEnumerator 绘制线段(Texture2D 图片,PosColor[] 坐标颜色,int 笔尖像素,Action 回调)
    {
        yield return 0;
        int 计数 = 0;
        for (int i = 1; i < 坐标颜色.Length; i++)
        {
            float 距离 = (坐标颜色[i - 1].pos - 坐标颜色[i].pos).magnitude + 1;
            for (int f = 0; f<=距离;f++)
            {
                Vector2 temp = Vector2.Lerp(坐标颜色[i - 1].pos, 坐标颜色[i].pos, f/距离);

                PosColor[] 缓存 = 笔尖坐标(笔尖像素, new PosColor(temp.ToVector2Int(), 坐标颜色[i].color));
                foreach (var item in 缓存)
                {
                    计数++;
                    if (计数 >= 5000)
                    {
                        计数 = 0;
                        yield return 0;
                    }
                    图片.SetPixel(item.pos.x, item.pos.y, item.color);
                }
            }
        }
        图片.Apply();
        回调?.Invoke();
    }

    public static IEnumerator 倒墨工具(Texture2D 图片,Color 颜色,Action 回调)
    {
        yield return 0;
        int 计数 = 0;
        for (int x = 0; x < 图片.width; x++)
        {
            for (int y = 0; y < 图片.height; y++)
            {
                计数++;
                if (计数 >= 20000)
                {
                    yield return 0;
                    计数 = 0;
                }
                图片.SetPixel(x, y, 颜色);
            }
        }
        图片.Apply();
        回调?.Invoke();
    }

    /// <summary>
    /// 由于是类,只需要转一次 Sprite 内部贴图将指向 设置的贴图
    /// </summary>
    /// <param name="图片"></param>
    /// <param name=""></param>
    /// <returns></returns>
    public static Sprite 贴图转精灵(Texture2D 图片)
    {
        return Sprite.Create(图片, new Rect(0, 0, 图片.width, 图片.height), Vector2.zero);
    }
}
